Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Views

Public Class AddUserDialog

    Public Event UserAddedToGroup(ByVal user As User)
    Public Event UsersAddedToGroup(ByVal users As IList(Of User))

#Region "IAddUserDialogView"

    Private _groupId As Guid
    Public Property GroupId() As Guid
        Get
            Return _groupId
        End Get
        Set(ByVal value As Guid)
            _groupId = value
        End Set
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

    Public Sub AddUsers(ByVal users As IEnumerable(Of User))

        lstUsers.DisplayMember = "NameAndUser"

        For Each u As User In users
            lstUsers.Items.Add(New UserInfo(u.Id, String.Format("{0} {1} - ({2})", u.Name, u.Name, u.UserName)))
        Next

    End Sub

    Public Sub RaiseUsersAddedEvent(ByVal users As List(Of User))
        RaiseEvent UsersAddedToGroup(users)
    End Sub

#End Region

    Public Sub New(ByVal groupId As Guid)

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.GroupId = groupId

            Dim groupRepository As New GroupRepository()
            Dim group = groupRepository.GetById(groupId)

            Me.FormTitle = String.Format("Adding Users to Group: {0}", group.Name)

            Dim users = Me.GetAllUsersNotInGroup(group) 'ToDo: Refactor!
            Me.AddUsers(users)

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Private Function GetAllUsersNotInGroup(ByVal group As Group) As IEnumerable(Of User)

        Dim result As New List(Of User)()

        Dim userRepository As New UserRepository()

        For Each u As User In userRepository.GetAll()
            If Not group.Users.Contains(u) Then
                result.Add(u)
            End If
        Next

        Return result

    End Function

    Private Sub lstUsers_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim userId As Guid = CType(lstUsers.SelectedItem, UserInfo).Id

        Dim addedUser As User = Me.AddUserToGroup(userId)
        RaiseEvent UserAddedToGroup(addedUser)

        Me.Close()

    End Sub

    <Obsolete()> _
        Public Function AddUserToGroup(ByVal userId As Guid) As User

        Dim groupRepository As New GroupRepository()
        Dim userRepository As New UserRepository()

        Dim groupId As Guid = Me.GroupId
        Dim groupToUpdate As Group = groupRepository.GetById(groupId)
        Dim user As User = userRepository.GetById(userId)

        groupToUpdate.AddUser(user)
        groupRepository.SaveOrUpdate(groupToUpdate)

        Return user

    End Function

    Public Sub AddUsersToGroup(ByVal userIds As IList(Of Guid))

        Dim groupRepository As New GroupRepository()
        Dim groupToUpdate = groupRepository.GetById(Me.GroupId)

        Dim userRepository As New UserRepository()
        Dim user As User
        Dim userList As New List(Of User)

        For Each id As Guid In userIds

            user = userRepository.GetById(id)
            userList.Add(user)

            groupToUpdate.Users.Add(user)
            groupRepository.SaveOrUpdate(groupToUpdate)

        Next

        Me.RaiseUsersAddedEvent(userList)

    End Sub

    Private Class UserInfo

        Public Sub New(ByVal id As Guid, ByVal nameAndUser As String)
            Me.Id = id
            Me.NameAndUser = nameAndUser
        End Sub

        Private _id As Guid
        Public Property Id() As Guid
            Get
                Return _id
            End Get
            Set(ByVal value As Guid)
                _id = value
            End Set
        End Property


        Private _nameAndUser As String
        Public Property NameAndUser() As String
            Get
                Return _nameAndUser
            End Get
            Set(ByVal value As String)
                _nameAndUser = value
            End Set
        End Property

    End Class

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim selectedItems As ListBox.SelectedObjectCollection = lstUsers.SelectedItems

        Dim userIds As New List(Of Guid)

        For Each ui As UserInfo In selectedItems
            userIds.Add(ui.Id)
        Next

        Me.AddUsersToGroup(userIds)

        Me.Close()

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class